const sip:any = require('sip');
import { debuglog,inspect } from 'util';

const SIP_DEBUG:boolean = true;

function rstring():string { return Math.floor(Math.random()*1e6).toString(); }

let current_cmd_sn = 1;
const common:any = {
    current_sn:1,
    SIP_REGISTER: "REGISTER",
    SIP_MESSAGE:"MESSAGE",
    CMD_CATALOG:"Catalog",
    CONTENT_MANSCDP:"Application/MANSCDP+xml",
    messages:{
        "200":"OK",
        "401":"Unauthorized"
    },
    sn: function():string{
        let ret = current_cmd_sn++;
        //TODO 处理溢出
        return ret.toString();
    },
    tag: function():string{
        return rstring();
    },
    callId: function():string{
        return rstring();
    }
}

const option:any = {
    logger: { 
        send: function(message:any, address:any) {
            // debugger; 
            debuglog("send\n" + inspect(message, false, null));
        },
        recv: function(message:any, address:any) {
            // debugger;
            debuglog("recv\n" + inspect(message, false, null));
        }

    },
    // address:"0.0.0.0",
    port:25060,
    publicAddress:"10.0.0.215",
    // hostname:"LYLPC"
};

const printfSipMessage = function(message:any,title:string):void{
    console.log("===================================================================");
    console.log(title);
    console.log("===================================================================");
    console.log(sip.stringify(message));
    console.log("===================================================================");
};

sip.start(option, async (request:any) => {
    if(SIP_DEBUG) printfSipMessage(request,"REQUEST >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
    switch (request.method)
    {
        case common.SIP_REGISTER:
            
            break;
        case common.SIP_MESSAGE:
            try {
                console.log("message");
                //TODO message
                let response = sip.makeResponse(request, 200, common.messages[200]);
                if(SIP_DEBUG) printfSipMessage(response,"RESPONSE <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
                sip.send(response);
            } catch (e) {
                console.log(e.stack);
            }
            break;
        case common.SIP_INVITE:
            console.log("invite");
            //TODO invite
            break;
        case common.SIP_ACK:
            console.log("ack");

            //TODO ack
            break;
        case common.SIP_BYE:
            console.log("bye");
            //TODO bye
            break;
        default:
            console.log('unsupported: ' + request.method);
            break;
    }
});

let message = {
    "method": "MESSAGE",
    "uri": "sip:41010200001320000001@127.0.0.1:18080",
    "version": "2.0",
    "headers": {
      "from": {
        "uri": "sip:41010200002000000001@127.0.0.1:25060",
        "params": {
          "tag": "249948303"
        }
      },
      "to": {
        "uri": "sip:41010200001320000001@127.0.0.1:18080",
        "params": {
        }
      },
      "call-id": "1888597076@127.0.0.1",
      "cseq": {
        "seq": 100028,
        "method": "MESSAGE"
      },
    //   "content-type:": "Application/MANSCDP+xml",
      "max-forwards": "70",
    //   "content-length": 3
    },
    "content": "abc"
} 

// printfSipMessage(message,"message");
sip.send(message);


console.log(JSON.stringify(sip.parseUri("sip:41010200001320000001@10.0.1.100:5060")));
console.log(JSON.stringify(sip.parseUri("sip:34020000001320000001@10.0.1.100:5060")));